﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace Boxie
{
    public partial class frmBoxie : Form
    {
        private string clipboardTemp;
        private bool TERMINATE = false;
        public static frmBoxie instance;
        public string[] appargs;
        public string appnamebuild;
        public frmLog fL;
        public frmQueue fQ;
        public frmSmartTyping fS;
        public frmDrawing fD;
        public frmMusicTag fM;
        public frmChangelog fC;
        public frmTextEdit fT;
        public bool enableSmartTyping;
        public int SnapDist;

        public frmBoxie(string[] args)
        {
            InitializeComponent();
            appargs = new string[] { };
            appargs = BoxieLib.MergeArrays(appargs, args);
            appnamebuild = "Boxie, build " + Boxie.Program.BUILD + " - ";
            frmBoxie.instance = this;
            enableSmartTyping = BoxieRegLib.GetSettingBool("General/SmartTyping", true);
            SnapDist = Convert.ToInt32(BoxieRegLib.GetSettingString("General/SnapDistance", "100"));
            if (BoxieRegLib.GetSettingBool("General/WindowTopMost", true))
                this.TopMost = true;
            // Check for auto doing certain actions
            if (appargs.Length >= 1)
            {
                if (Boxie.BoxieLib.allFileType(".mp3"))
                {
                    if (BoxieRegLib.GetSettingBool("Auto/AutoMusicTag", false))
                    {
                        bool success = Boxie.BoxieMusicLib.DoTagging(appargs, BoxieRegLib.GetSettingString("Auto/MusicFormat", "%13% - %21%.%ext%"));
                        TERMINATE = success;
                    }
                }
            }
        }

        private bool DoSnap(int pos, int edge)
        {
            int delta = pos - edge;
            return delta > 0 && delta <= SnapDist;
        }

        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResizeEnd(e);
            if (!BoxieRegLib.GetSettingBool("General/SnapToEdge", true))
                return;
            Screen scn = Screen.FromPoint(this.Location);
            // Actual Screen
            if (DoSnap(this.Left, scn.WorkingArea.Left)) this.Left = scn.WorkingArea.Left;
            if (DoSnap(this.Top, scn.WorkingArea.Top)) this.Top = scn.WorkingArea.Top;
            if (DoSnap(scn.WorkingArea.Right, this.Right)) this.Left = scn.WorkingArea.Right - this.Width;
            if (DoSnap(scn.WorkingArea.Bottom, this.Bottom)) this.Top = scn.WorkingArea.Bottom - this.Height;
            // Off-Screen
            if (DoSnap(this.Left, scn.WorkingArea.Left - SnapDist)) this.Left = scn.WorkingArea.Left;
            if (DoSnap(this.Top, scn.WorkingArea.Top - SnapDist)) this.Top = scn.WorkingArea.Top;
            if (DoSnap(scn.WorkingArea.Right, this.Right - SnapDist)) this.Left = scn.WorkingArea.Right - this.Width;
            if (DoSnap(scn.WorkingArea.Bottom, this.Bottom - SnapDist)) this.Top = scn.WorkingArea.Bottom - this.Height;
        }

        public void SetTextBox(string s, bool push)
        {
            this.txtArguments.Text = s;
            if (push)
                this.txtArguments_KeyUp(this, new KeyEventArgs(Keys.Return));
        }

        private void frmPoof_Load(object sender, EventArgs e)
        {
            this.Text = appnamebuild + appargs.Length + " Files Loaded";
            txtArguments.SelectionStart = 0;
            if (BoxieRegLib.GetSettingBool("General/LogVisible", false))
            {
                fL = new frmLog(this);
                fL.Left = 1;
                fL.Show();
            }
            if (BoxieRegLib.GetSettingBool("General/CheckUpdates", true))
                BoxieThreadLib.CheckForUpdates();
        }

        private void ProcessFile_Files(string f)
        {
            try
            {
                string[] actions = txtArguments.Tag.ToString().Split(' ');
                foreach (string act in actions)
                {
                    string xact = act.ToLower();
                    if (xact == "upload")
                    {
                        // init
                        fL.add("Uploading file to FTP...", 5);
                        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://" + BoxieRegLib.GetSettingString("FTP/Hostname", "ftp.example.com") + BoxieRegLib.GetSettingString("FTP/FtpLocation", "/some/dir/") + Path.GetFileName(f));
                        request.Method = WebRequestMethods.Ftp.UploadFile;
                        request.Credentials = new NetworkCredential(BoxieRegLib.GetSettingString("FTP/Username", ""), BoxieRegLib.GetSettingString("FTP/Password", ""));
                        request.UsePassive = true;
                        request.UseBinary = true;
                        request.KeepAlive = false;
                        // load into memory
                        FileStream stream = File.OpenRead(f);
                        byte[] buffer = new byte[stream.Length];
                        stream.Read(buffer, 0, buffer.Length);
                        stream.Close();
                        // upload
                        Stream reqStream = request.GetRequestStream();
                        reqStream.Write(buffer, 0, buffer.Length);
                        reqStream.Close();
                        // saving final link to clipboard?
                        if (BoxieRegLib.GetSettingBool("FTP/Clipboard", true))
                        {
                            if (this.clipboardTemp != null)
                                this.clipboardTemp += Environment.NewLine;
                            this.clipboardTemp += BoxieRegLib.GetSettingString("FTP/WebLocation", "http://www.example.com/some/dir/") + Path.GetFileName(f);
                        }
                    }
                    if ((xact == "delete") || (xact == "del"))
                        System.IO.File.Delete(f);
                }
            }
            catch(Exception e)
            {
                fL.add(">> An error has occured on this file! Double click for more info.", 3, e.Message);
            }
            
        }
        private void ProcessFile_Images(string f)
        {
            try
            {
                EncoderParameter ep = new EncoderParameter(Encoder.Quality, 50);
                EncoderParameters eps = new EncoderParameters(1);
                eps.Param[0] = ep;
                FileStream fs = new FileStream(f, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                Image i = Image.FromStream(fs);
                fs.Close();

                string[] actions = txtArguments.Tag.ToString().Split(' ');
                string suffix = "";
                foreach (string act in actions)
                {
                    string xact = act.ToLower();
                    if (xact == "flipx")
                        i.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    if (xact == "flipy")
                        i.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    if (xact == "flipxy")
                        i.RotateFlip(RotateFlipType.RotateNoneFlipXY);
                    if (xact == "rotate90")
                        i.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    if (xact == "rotate180")
                        i.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    if (xact == "rotate270")
                        i.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    if(xact.Contains("scale:"))
                    {
                        float perc = Convert.ToSingle(xact.Substring(6));
                        if ((perc > 0) && (perc < 1000))
                        {
                            float newHeight = (float)i.Height * ((float)0.01 * perc);
                            float newWidth = (float)i.Width * ((float)0.01 * perc);

                            i = BoxieLib.ResizeImage(i, (int)newWidth, (int)newHeight);
                        }
                    }
                    if (xact.Contains("suffix:"))
                    {
                        suffix = xact.Substring(7);
                    }
                    if (xact.Contains("osuffix:"))
                    {
                        string osuffix = xact.Substring(8);
                        System.IO.File.Move(f, System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + osuffix + System.IO.Path.GetExtension(f));
                    }
                    if (xact.Contains("width:"))
                    {
                        int width = Convert.ToInt32(xact.Substring(6));
                        if(width>0)
                            i = BoxieLib.ResizeImage(i, width, i.Height);
                    }
                    if (xact.Contains("height:"))
                    {
                        int height = Convert.ToInt32(xact.Substring(7));
                        if(height>0)
                            i = BoxieLib.ResizeImage(i, i.Width, height);
                    }
                    if ((xact == "grayscale") || (xact == "gs"))
                        i = BoxieLib.MakeGrayscale(new Bitmap(i));
                    if ((xact == "delete") || (xact == "del"))
                        System.IO.File.Delete(f);
                    if (xact == "save")
                    {
                        System.IO.File.Delete(f);
                        if (System.IO.Path.GetExtension(f).ToLower() == ".jpg") i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                        if (System.IO.Path.GetExtension(f).ToLower() == ".jpeg") i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
                        if (System.IO.Path.GetExtension(f).ToLower() == ".png") i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".png", System.Drawing.Imaging.ImageFormat.Png);
                        if (System.IO.Path.GetExtension(f).ToLower() == ".gif") i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                        if (System.IO.Path.GetExtension(f).ToLower() == ".ico") i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".ico", System.Drawing.Imaging.ImageFormat.Icon);
                        if (System.IO.Path.GetExtension(f).ToLower() == ".bmp") i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                        if (System.IO.Path.GetExtension(f).ToLower() == ".tiff") i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".tiff", System.Drawing.Imaging.ImageFormat.Tiff);
                    }
                    if ((xact == "savejpg")||(xact == "savejpeg"))
                        i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    if (xact == "savepng")
                        i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".png", System.Drawing.Imaging.ImageFormat.Png);
                    if (xact == "savegif")
                        i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                    if (xact == "saveico")
                        i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".ico", System.Drawing.Imaging.ImageFormat.Icon);
                    if (xact == "savebmp")
                        i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    if (xact == "savetiff")
                        i.Save(System.IO.Path.GetDirectoryName(f) + "\\" + System.IO.Path.GetFileNameWithoutExtension(f) + suffix + ".tiff", System.Drawing.Imaging.ImageFormat.Tiff);
                }
                i.Dispose();
            }
            catch (Exception e)
            {
                fL.add(">> An error has occured on this file! Double click for more info.", 3, e.Message);
            }
        }

        private void txtArguments_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtArguments.ReadOnly)
                return;

            if (e.KeyCode == Keys.Tab)
            {
                if (fS != null)
                    if (fS.Visible)
                        fS.autoComplete(txtArguments);
            }
            else if (e.KeyCode == Keys.Return)
            {
                if (txtArguments.Text == "")
                    return;

                if ((txtArguments.Text.ToLower() == "exit") || (txtArguments.Text.ToLower() == "quit") || (txtArguments.Text.ToLower() == "x"))
                {
                    Application.Exit();
                }
                else if (txtArguments.Text.ToLower() == "clear")
                {
                    this.ClearEntireQueue();
                    txtArguments.Text = "";
                }
                else if (txtArguments.Text.ToLower() == "about")
                {
                    frmAbout f = new frmAbout();
                    f.lBuild.Text = "Build " + Boxie.Program.BUILD;
                    f.Show();
                    txtArguments.Text = "";
                }
                else if ((txtArguments.Text.ToLower() == "queue")||(txtArguments.Text.ToLower() == "q"))
                {
                    fQ = new frmQueue(this);
                    fQ.Show();
                    txtArguments.Text = "";
                }
                else if (txtArguments.Text.ToLower() == "config")
                {
                    frmConfig f = new frmConfig();
                    txtArguments.Text = "";
                    f.ShowDialog();
                }
                else if (txtArguments.Text.ToLower() == "md5hash")
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    if (appargs.Length == 0)
                    {
                        fL.add("There are no files in the queue.", 3, "No additional information available.");
                    }
                    else
                    {
                        BoxieHashLib.DumpMD5Hashes();
                        fL.add("Dumped md5 hashes into text file.", 4);
                    }
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if ((txtArguments.Text.ToLower() == "changelog")||(txtArguments.Text.ToLower() == "changes"))
                {
                    if (fC == null || fC.IsDisposed )
                        fC = new frmChangelog();
                    fC.Show();
                    fC.getChanges();
                    txtArguments.Text = "";
                    fC.Focus();
                }
                else if (txtArguments.Text.ToLower() == "textedit")
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    if (BoxieWebLib.socket == null)
                    {
                        fL.add("You must be connected to another Boxie first!", 3, "You must first connect to another Boxie user via the LISTEN or CONNECT command.");
                    }
                    else
                    {
                        if (fT == null || fT.IsDisposed)
                            fT = new frmTextEdit();
                        fT.Show();
                        BoxieWebLib.SendToBoxie("TESTART");
                        frmBoxie.instance.fL.add("Text editing session started.", 2);
                    }
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if (txtArguments.Text.ToLower() == "update")
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    fL.add("Update started.", 2);
                    fL.add("Downloading executable..", 2);
                    System.IO.File.Delete(System.IO.Path.GetTempPath() + "Boxie.exe");
                    WebClient wc = new WebClient();
                    wc.DownloadFile("http://www.ethryx.net/boxie/Boxie.exe", System.IO.Path.GetTempPath() + "Boxie.exe");
                    fL.add("Update Successful! Restarting Boxie..", 4);
                    System.Diagnostics.Process.Start(System.IO.Path.GetTempPath() + "Boxie.exe", "-update \"" + Application.ExecutablePath + "\"");
                    System.Threading.Thread.Sleep(1000);
                    Application.Exit();
                }
                else if ((txtArguments.Text.ToLower() == "webdir") || (txtArguments.Text.ToLower() == "www"))
                {
                    string www = BoxieRegLib.GetSettingString("FTP/WebLocation", "");
                    if (www != "")
                        System.Diagnostics.Process.Start(www);
                    txtArguments.Text = "";
                }
                else if ((txtArguments.Text.ToLower() == "clipboard") || (txtArguments.Text.ToLower() == "cb"))
                {
                    string filepath_img = System.IO.Path.GetTempPath() + "img_" + DateTime.Now.ToFileTimeUtc().ToString() + ".png";
                    string filepath_txt = System.IO.Path.GetTempPath() + "text_" + DateTime.Now.ToFileTimeUtc().ToString() + ".txt";
                    if (Clipboard.ContainsImage())
                        BoxieLib.SaveClipboardImage(this, filepath_img);
                    else if (Clipboard.ContainsText())
                        BoxieLib.SaveClipboardText(this, filepath_txt);
                    else
                    {
                        if (fL == null)
                            fL = new frmLog(this);
                        fL.Left = 1;
                        fL.Show();
                        fL.add("There is no image or text on the clipboard.", 3, "No additional information available.");
                    }

                    txtArguments.Text = "";
                }
                else if ((txtArguments.Text.ToLower().Contains("clipboard:")) || (txtArguments.Text.ToLower().Contains("cb:")))
                {
                    try
                    {
                        string filename = txtArguments.Text.Split(':')[1];
                        string filepath = System.IO.Path.GetTempPath() + filename + ".png";
                        BoxieLib.SaveClipboardImage(this, filepath);
                    }
                    catch
                    {
                        if (fL == null)
                            fL = new frmLog(this);
                        fL.Left = 1;
                        fL.Show();
                        fL.add("There is no image on the clipboard.", 3, "No additional information available.");
                    }

                    txtArguments.Text = "";
                }
                else if (txtArguments.Text.ToLower() == "send")
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    if (BoxieWebLib.socket == null)
                        fL.add("You must be connected to another Boxie first!", 3, "You must first connect to another Boxie user via the LISTEN or CONNECT command.");
                    else if (appargs.Length == 0)
                        fL.add("There are no files in the queue.", 3, "No additional information available.");
                    else
                    {
                        BoxieWebLib.SendFilesInQueue();
                        ClearEntireQueue();
                    }
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if (txtArguments.Text.ToLower() == "zip")
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    if (appargs.Length == 0)
                        fL.add("There are no files in the queue.", 3, "No additional information available.");
                    else
                        BoxieLib.ZipQueuedFiles(Application.StartupPath + "\\zip_" + DateTime.Now.ToFileTimeUtc().ToString() + ".zip");
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if (txtArguments.Text.ToLower().Contains("zip:"))
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    if (appargs.Length == 0)
                        fL.add("There are no files in the queue.", 3, "No additional information available.");
                    else
                        BoxieLib.ZipQueuedFiles(Application.StartupPath + "\\" + txtArguments.Text.Split(':')[1] + ".zip");
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if (txtArguments.Text.ToLower() == "tagmusic")
                {
                    if (appargs.Length == 0)
                    {
                        if (fL == null)
                            fL = new frmLog(this);
                        fL.Left = 1;
                        fL.Show();
                        fL.add("There are no files in the queue.", 3, "No additional information available.");
                    }
                    else
                    {
                        if (fM == null)
                            fM = new frmMusicTag();
                        fM.Visible = true;
                        fM.PopulateList();
                    }
                    txtArguments.Text = "";
                    txtArguments.Focus();
                    fM.Focus();
                }
                else if (txtArguments.Text.ToLower() == "draw")
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    fL.add("This command is disabled in this version.", 3, "The functionality of this command is not complete, so it is not available in this build.");
                    /*if (BoxieWebLib.socket == null)
                        fL.add("You must be connected to another Boxie first!", 3, "You must first connect to another Boxie user via the LISTEN or CONNECT command.");
                    else
                    {
                        if (fD == null)
                            fD = new frmDrawing();
                        fD.Show();
                        fD.Text = "Drawing with " + BoxieWebLib.socket.RemoteEndPoint.ToString();
                        BoxieWebLib.SendToBoxie("DRAWSTART");
                        frmBoxie.instance.fL.add("Drawing session started.", 2);
                    }*/
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if (txtArguments.Text.ToLower() == "listen")
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    BoxieWebLib.ListenMode(2123);
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if (txtArguments.Text.ToLower().Contains("listen:"))
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    int port = Convert.ToInt32(txtArguments.Text.Split(':')[1]);
                    BoxieWebLib.ListenMode(port);
                    txtArguments.Text = "";
                    txtArguments.Focus();
                }
                else if (txtArguments.Text.ToLower().Contains("connect:"))
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    string ip = BoxieLib.CheckIPAlias(txtArguments.Text.Split(':')[1]);
                    if (txtArguments.Text.Split(':').GetUpperBound(0) == 2)
                        BoxieWebLib.ConnectMode(ip, Convert.ToInt32(txtArguments.Text.Split(':')[2]));
                    else
                        BoxieWebLib.ConnectMode(ip, 2123);
                    txtArguments.Text = "";
                }
                else if (txtArguments.Text.ToLower() == "help")
                {
                    System.Diagnostics.Process.Start("http://boxie.codeplex.com/documentation");
                    txtArguments.Text = "";
                }
                else if ((appargs.Length >= 1) && (txtArguments.Text != ""))
                {
                    if (fL == null)
                        fL = new frmLog(this);
                    fL.Left = 1;
                    fL.Show();
                    if (appargs.Length == 1)
                        fL.add("Boxie started (" + appargs.Length.ToString() + " file):", 2);
                    else
                        fL.add("Boxie started (" + appargs.Length.ToString() + " files):", 2);
                    txtArguments.ReadOnly = true;
                    txtArguments.Tag = txtArguments.Text + " ";
                    for (int x = 0; x < appargs.Length; x++)
                    {
                        int filetype = BoxieLib.ReturnFileType(appargs[x]);
                        if (filetype == 1)
                        {
                            fL.add("Image: " + appargs[x], 1);
                            ProcessFile_Images(appargs[x]);
                        }
                        if (filetype == 2)
                        {
                            fL.add("File: " + appargs[x], 0);
                        }
                        ProcessFile_Files(appargs[x]);
                    }
                    fL.add("Boxie operations have finished!", 4);
                    if (this.clipboardTemp != null)
                        Clipboard.SetText(this.clipboardTemp);
                    this.clipboardTemp = null;
                    if ((BoxieRegLib.GetSettingBool("General/AutoClose", false)) && (!fL.errorOccurred))
                    {
                        fL.add("Boxie will automatically close in a second..", 4);
                        System.Threading.Thread.Sleep(1500);
                        Application.Exit();
                    }
                    this.ClearEntireQueue();
                    txtArguments.ReadOnly = false;
                    txtArguments.Text = "";
                    txtArguments.Tag = "";
                    txtArguments.Focus();
                }
                else if ((appargs.Length == 0) && (txtArguments.Text != ""))
                {
                    txtArguments.Text = "";
                    MessageBox.Show("You must add one or more files to the queue.\nDrag and drop them to the text box, or type queue to manage the queue.", "Empty Queue", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void txtArguments_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                e.Effect = DragDropEffects.All;
        }

        private void txtArguments_DragDrop(object sender, DragEventArgs e)
        {
            string[] droppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
            appargs = BoxieLib.MergeArrays(appargs, droppedFiles);
            if (fQ != null)
            {
                if (fQ.Visible)
                {
                    fQ.lst.Items.Clear();
                    for (int x = 0; x <= appargs.GetUpperBound(0); x++)
                        fQ.lst.Items.Add(appargs[x]);
                }
            }
            
            this.Text = appnamebuild + appargs.Length + " Files Loaded";
            this.Focus();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtArguments_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtArguments.Text == "Start typing commands here or help for more info.")
            {
                txtArguments.Text = "";
                txtArguments.Font = new Font("Candara", 10, FontStyle.Bold);
                txtArguments.ForeColor = Color.Black;
            }
        }

        private void frmBoxie_Move(object sender, EventArgs e)
        {
            if (fL != null)
            {
                if (fL.Visible)
                {
                    fL.Top = this.Top + this.Height + 10;
                    fL.Left = this.Left;
                }
            }
            if (fS != null)
            {
                if (fS.Visible)
                {
                    fS.RepositionWindow(this);
                }
            }
        }

        public void ClearEntireQueue()
        {
            this.Text = appnamebuild + "0 Files Loaded";
            appargs = new string[] { };
            if (fQ != null)
                if (fQ.Visible)
                    fQ.lst.Items.Clear();
        }

        private void txtArguments_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (!enableSmartTyping)
                    return;
                string[] cmdarray = txtArguments.Text.Split(' ');
                string lastcmd = cmdarray[cmdarray.GetUpperBound(0)];
                if (lastcmd == "")
                {
                    if (fS.Visible)
                        fS.Hide();
                    return;
                }
                // is smart window open?
                if (fS == null)
                    fS = new frmSmartTyping();
                else if (fS.txt.Text != "")
                {
                    fS.txt.Text = "";
                    fS.lockRtb(true);
                }
                fS.Left = this.Left;
                BoxieLib.SmartTyping(fS,lastcmd, this);
                if (fS.txt.Text != "")
                {
                    fS.Show();
                    this.Focus();
                    txtArguments.SelectionStart = txtArguments.TextLength;
                }
                else if(fS.Visible)
                    fS.Hide();
            }
            catch
            {
            }
        }

        private void frmBoxie_Shown(object sender, EventArgs e)
        {
            if (TERMINATE)
                Application.Exit();
        }
    }
}
